#!/usr/bin/python

"""
Pacmon, an updates notification tray application for the users of Arch Linux
Copyright 2006 Matt McDonald <metzen@gmail.com>

This file is part of Pacmon.

Pacmon is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

Pacmon is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
"""

import sys

VERBOSE = False

class MessageQueue(object):
    def __init__(self, verbose=False):
        globals()['VERBOSE'] = verbose
        self.message_queue = []

    def __add__(self, message):
        self.message_queue += [message]
        if VERBOSE:
            sys.stdout.write(message)
            sys.stdout.flush()

    def write(self, message):
        self.message_queue += [message]
        if VERBOSE:
            sys.stdout.write(message)
            sys.stdout.flush()

    def read_message(self):
        if len(message_queue) == 0:
            return None
        return self.message_queue.pop(0)

    def read_messages(self):
        for i in range(len(self.message_queue)):
            yield self.message_queue.pop(0)

    def flush(self):
        sys.stdout.flush()

    def length(self):
        return len(self.message_queue)
